<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'员工列表'),
	array('op'=>'list1','title'=>'申请审核列表'),
	array('op'=>'edit','title'=>'添加员工'),	
);
$merchid=$this->checklogin();
$merch=pdo_get('jy_gohome_merch',array('id'=>$merchid));
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['examine']=1;
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(isset($_RQ['phone'])){
		$params['phone']=intval($_RQ['phone']);
		$return['params']['phone']=$_RQ['phone'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','name','phone','commission','lev','years','iswx','age','headimg','zhengjian','service','remark','islock','isshow','status','address','createtime');
	$list=pdo_getslice('jy_gohome_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if(empty($list)){
		$list = array(
			array('name'=>'技师小娜','id'=>1)
		);
	}
	if($list){
		foreach($list as &$item){
			$item['lev_name']=pdo_getcolumn('jy_gohome_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['lev']),'lev_name');
			if(!$item['lev_name']){
				$item['lev_name']='未设置';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>'员工姓名','type'=>'string'),
			array('name'=>'headimg','title'=>'员工头像','type'=>'image'),
			array('name'=>'phone','title'=>'电话','type'=>'string'),
			//array('name'=>'lev','title'=>'等级','type'=>'string'),
			array('name'=>'lev_name','title'=>'等级名称','type'=>'string'),
			array('name'=>'commission','title'=>'佣金','type'=>'string'),
			array('name'=>'isshow','title'=>'是否首页','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'islock','title'=>'是否锁定','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'name','width'=>15,'title'=>'员工姓名','type'=>'string'),
			array('name'=>'phone','width'=>15,'title'=>'电话','type'=>'string'),
			array('name'=>'status','width'=>18,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['htmls']=$htmls;
	$return['status2']=array(array('id'=>0,'title'=>'未启用'),array('id'=>0,'title'=>'已启用'));
	set_html('framess',$this->menu());
	exi($return);
}
if($_RQ['op']=='list1'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['examine']=0;//待审核
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(isset($_RQ['phone'])){
		$params['phone']=intval($_RQ['phone']);
		$return['params']['phone']=$_RQ['phone'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','name','phone','lev','years','commission','iswx','age','headimg','zhengjian','service','remark','islock','isshow','status','address','createtime');
	$list=pdo_getslice('jy_gohome_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){

		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>'员工姓名','type'=>'string'),
			array('name'=>'headimg','title'=>'员工头像','type'=>'image'),
			array('name'=>'phone','title'=>'电话','type'=>'string'),
			//array('name'=>'lev','title'=>'等级','type'=>'string'),
			array('name'=>'lev_name','title'=>'等级名称','type'=>'string'),
			array('name'=>'zhengjian','title'=>'身份证号','type'=>'string'),
			//array('name'=>'service','title'=>'可服务项目','type'=>'string'),
			array('name'=>'isshow','title'=>'是否首页','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'islock','title'=>'是否锁定','type'=>'switch','set'=>'set'),
			array('name'=>'examine','title'=>'是否通过审核','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'name','title'=>'员工姓名','type'=>'string'),
			array('name'=>'phone','title'=>'电话','type'=>'string'),
			array('name'=>'zhengjian','title'=>'身份证号','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_gohome_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回员工列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'员工姓名','type'=>'string'),
					array('name'=>'headimg','title'=>'员工头像','type'=>'image'),
					array('name'=>'phone','title'=>'电话','type'=>'string','help'=>''),
					array('name'=>'lev','title'=>'等级名称','type'=>'select','options'=>'lev_name','option_title'=>'lev_name'),
					array('name'=>'years','title'=>'从事行业年限','type'=>'string'),
					array('name'=>'mid','title'=>'绑定员工会员id','type'=>'string','help'=>'填系统会员ID，<a href="./index.php?plugin=core&action=member.member" target="_blank">点击~快速获取会员ID</a>'),
					array('name'=>'age','title'=>'年纪','type'=>'string'),
					array('name'=>'address','title'=>'地址','type'=>'string'),
					array('name'=>'service','title'=>'可服务项目','type'=>'checkbox','options'=>'service'),
					array('name'=>'isshow','title'=>'是否首页','type'=>'switch'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
					array('name'=>'islock','title'=>'是否锁定','type'=>'switch'),
					array('name'=>'examine','title'=>'审核通过','type'=>'switch'),
					array('name'=>'remark','title'=>'员工介绍','type'=>'text'),
				)
			),
		)
	);
	$service=pdo_getall('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	foreach($service as &$row){
		$row=$row['title'];
	}
	$return['service']=$service;
	$return['lev_name']=pdo_getall('jy_gohome_lev',array('uniacid'=>$_SESSION['uniacid']),array('id','lev_name'),'id');
	$return['htmls']=$htmls;
	//print_r($return['lev_name']);exit;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	//exi($postdata['lev_name'],'error');exit;
	$data=array(
		'name'=>trim($postdata['name']),
		'merchid'=>$merchid,
		'phone'=>trim($postdata['phone']),
		'province'=>$merch['province'],
		'city'=>$merch['city'],
		'headimg'=>trim($postdata['headimg']),
		'lev'=>intval($postdata['lev']),
		'years'=>intval($postdata['years']),
		'age'=>intval($postdata['age']),
		'service'=>trim($postdata['service']),
		'remark'=>trim($postdata['remark']),
		'status'=>intval($postdata['status']),
		'address'=>trim($postdata['address']),
		'mid'=>intval($postdata['mid']),
		'isshow'=>intval($postdata['isshow']),
		'islock'=>intval($postdata['islock']),
		'examine'=>intval($postdata['examine']),
		'zhengjian'=>intval($postdata['zhengjian']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_gohome_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_gohome_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_gohome_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的员工！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_gohome_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isshow'])){
		pdo_update('jy_gohome_worker',array('isshow'=>intval($post['isshow'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['islock'])){
		pdo_update('jy_gohome_worker',array('islock'=>intval($post['islock'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['examine'])){
		pdo_update('jy_gohome_worker',array('examine'=>intval($post['examine'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}